package sm.hibernate.type;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;

import sm.codec.type.Operator;
import sm.codec.type.RC;

public class RCType extends AbstractRCCompositeType {

	@Override
	public Class returnedClass() {
		return Operator.class;
	}

	@Override
	public Object deepCopy(Object value) throws HibernateException {
		return value;

	}

	@Override
	protected Object rcToValue(RC rc, Object owner) {
		Object value = rc;
		if (rc != null) {
			List<Object> values = rc.getValues();
			if (values != null && values.size() == 1) {
				value = values.get(0);
			}
		}
		return value;
	}

	@Override
	protected RC valueToRC(Object value) {
		RC rc = null;
		if (value != null) {
			if (value instanceof RC) {
				rc = (RC) value;
			} else {
				List<Object> values = new ArrayList<Object>();
				values.add(value);
				rc = new RC(values);
			}
		}

		return rc;
	}

}
